package com.kitty.game.hunpo;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.message.RespSyncMessage;
import com.kitty.game.hunpo.message.ReqOpenPanel;
import com.kitty.game.hunpo.message.ReqUpHunPo;
import com.kitty.game.hunpo.service.HunPoService;
import com.kitty.game.player.model.HunPoProp;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * HunPoController 魂魄
 *
 * @author StanWind
 * @create 2020 - 04 - 07 12:14 AM
 */
@Controller
@Slf4j
public class HunPoController {


    @Autowired
    private RoleService roleService;

    @Autowired
    private HunPoService hunPoService;

    private static final String FULL_STAGE_MSG = "听闻#Y%s#n神魂突破圆满，真是我中州不可多得的人才！#3m";
    private static final String UP_STAGE_MSG = "听闻#Y%s#n神魂突破至#R%s阶#n，真是我中州不可多得的人才！#3m";

    /**
     * 打开魂魄面板
     *
     * @param session
     * @param request
     */
    @RequestMapping
    public void openPanel(IoSession session, ReqOpenPanel request) {
        Role role = SessionUtils.getRoleBySession(session);

        hunPoService.sendPanel(role);

        MessagePusher.pushMessage(session, new RespMsg("#R请注意内丹消耗积分！！！"));

    }

    /**
     * 升级魂魄
     *
     * @param session
     * @param request
     */
    @RequestMapping
    public void upHunPo(IoSession session, ReqUpHunPo request) {
        Role role = SessionUtils.getRoleBySession(session);
        HunPoProp hunPoProp = hunPoService.getHunPoProp(role);

        if (hunPoProp.getLevel() * 10 + hunPoProp.getLevelInner() >= 110) {
            MessagePusher.pushMessage(session, new RespMsg("已无法再突破！"));
            return;
        }

        //看当前需要的阴德
        int req = hunPoService.getStageUpReq(hunPoProp.getLevel(), hunPoProp.getLevelInner());
        if (req > hunPoProp.getYinDe()) {
            //阴德不够
            MessagePusher.pushMessage(session, new RespMsg("当前阴德不足以提升，请进入#R积分商城使用积分兑换阴德！"));
            return;
        }

        //减少阴德
        roleService.subYinde(role, req, false);




        RespSyncMessage respSyncMessage = new RespSyncMessage();
        respSyncMessage.setCode((short) 20481);

        //增加悟道等级
        if (hunPoProp.getLevelInner() >= 9) {
            hunPoProp.setLevelInner(0);
            hunPoProp.setLevel(hunPoProp.getLevel() + 1);
            if (hunPoProp.getLevel() > 10) {
                log.error("玩家{} 悟道升圆满{}", role.getUid(), hunPoProp.getLevel());
                hunPoProp.setLevelInner(10);
                hunPoProp.setLevel(10);
                respSyncMessage.setMsg(AsktaoUtil.getMessageBody(new RespNotifyMiscEx(
                        "你消耗了#R" + req + "#n阴德成功突破圆满!")));

                String msg = String.format(FULL_STAGE_MSG, role.getName());
                SpringUtils.getBroadcastService().sendRumor(msg, null);
            } else {
                log.error("玩家{} 悟道升大段{}", role.getUid(), hunPoProp.getLevel());
                respSyncMessage.setMsg(AsktaoUtil.getMessageBody(new RespNotifyMiscEx(
                        "你消耗了#R" + req + "#n阴德成功突破到" + hunPoProp.getLevel() + "阶!")));

                if (hunPoProp.getLevel() > 5) {
                    String msg = String.format(UP_STAGE_MSG, role.getName(), hunPoProp.getLevel());
                    SpringUtils.getBroadcastService().sendRumor(msg, null);
                }
            }
        } else {
            hunPoProp.setLevelInner(hunPoProp.getLevelInner() + 1);
            log.error("玩家{} 悟道升小段{}", role.getUid(), hunPoProp.getLevel());
            respSyncMessage.setMsg(AsktaoUtil.getMessageBody(new RespNotifyMiscEx(
                    "你消耗了#R" + req + "#n阴德成功突破!")));
        }

        //计算增加属性
        hunPoService.calcAddedProp(hunPoProp);
        role.save();
        //杂项
        MessagePusher.pushMessage(role, respSyncMessage);
        //弹窗
        MessagePusher.pushMessage(session, new RespMsg("恭喜，突破成功！"));

        //这三个包缺一不可，顺序也不能错
        //发送属性
        roleService.sendExtraAttribute(role);
        //更新阴德 stage
        roleService.sendUpdateShenHunStage(role);
        //更新面板
        hunPoService.sendPanel(role);
    }
}
